package com.common.base.app.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ScrollView
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.common.base.app.extras.*
import com.common.base.app.recycler.header.LoadingHeader
import com.common.base.utils.StatusBarUtils
import com.common.base.utils.ToastUtils
import com.common.base.widget.PlaceholderView
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshFooter
import com.scwang.smart.refresh.layout.api.RefreshHeader
import me.yokeyword.fragmentation.SupportFragment

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/19
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
abstract class BaseFragment : SupportFragment(), View.OnClickListener {

    final override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View {
        initViewPre()
        return getLayoutResView()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
        super.onCreate(savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        initEvents()
    }


    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        lazyInitData()
    }

    open fun lazyInitData() {

    }

    abstract fun getLayoutResView(): View

    open fun initViewPre() {
    }

    open fun initViews() {
        StatusBarUtils.setStatusBarForegroundColor(activity, isStatusBarForegroundBlack())
        initRefreshLayout()
    }

    open fun initEvents() {
        setErrorRetryListener { showEmpty() }
    }

    //占位控件,可以为空。为空不会显示占图位相关页面
    open fun getPlaceholderView(): PlaceholderView? = null

    open fun isShowLoading(): Boolean = true

    open fun isShowError(): Boolean = true

    open fun showLoading() {
        if (isShowLoading()) {
            showPageLoading(getPlaceholderView())
        }
    }

    open fun showContent() {
        showPageContent(getPlaceholderView())
    }

    open fun showError(msg: String? = null) {
        if (isShowError()) {
            showPageError(getPlaceholderView(), msg)
        }
    }

    open fun showErrorToast(msg: String? = null) {
        ToastUtils.showToast(msg)
    }

    protected open fun showEmpty(msg: String? = null) {
        showPageEmpty(getPlaceholderView(), msg)
    }

    protected open fun setErrorRetryListener(listener: () -> Unit) {
        setPageErrorRetryListener(getPlaceholderView(), listener)
    }

    override fun onClick(v: View?) {
    }

    /**
     * 子类提供一个刷新控件的引用
     */
    open fun getSmartRefreshLayout(): SmartRefreshLayout? = null

    /**
     * finishLoadMore是否延迟
     */
    open fun isDelayFinishLoadMore(): Boolean = true

    /**
     *  状态栏前景色是否是黑色
     */
    protected open fun isStatusBarForegroundBlack(): Boolean = true

    /**
     * 初始化刷新控件
     */
    private fun initRefreshLayout() {
        (getSmartRefreshLayout() == null).yes { return }
        setSmartLayoutAttrs()
        getRefreshHeader()?.let { getSmartRefreshLayout()?.setRefreshHeader(it) }
        getRefreshFooter()?.let { getSmartRefreshLayout()?.setRefreshFooter(it) }
        getSmartRefreshLayout()?.setOnRefreshListener {
            onRefreshListener()
        }
        getSmartRefreshLayout()?.setOnLoadMoreListener {
            onLoadMoreListener()
        }
    }

    /**
     * 设置SmartRefreshLayout相关属性
     */
    protected fun setSmartLayoutAttrs() {
        getSmartRefreshLayout()?.setEnableAutoLoadMore(isEnableAutoLoadMore())
        getSmartRefreshLayout()?.setEnableLoadMore(isEnableLoadMore())
        getSmartRefreshLayout()?.setEnableRefresh(isEnableRefresh())
        getSmartRefreshLayout()?.setEnableOverScrollBounce(isEnableOverScrollBounce())
        getSmartRefreshLayout()?.setEnablePureScrollMode(isEnablePureScrollMode())
        getSmartRefreshLayout()?.setEnableOverScrollDrag(isEnableOverScrollDrag())
        getSmartRefreshLayout()?.setEnableLoadMoreWhenContentNotFull(false)
    }

    //下拉刷新
    protected open fun onRefreshListener() {
        hideRefreshing(false)
    }

    //加载更多
    protected open fun onLoadMoreListener() {
        hideRefreshing(false)
    }


    /**
     * 获取下拉刷新的头部
     */
    protected open fun getRefreshHeader(): RefreshHeader? {
        return LoadingHeader(context)
    }

    /**
     * 获取刷新底部
     */
    protected open fun getRefreshFooter(): RefreshFooter? {
        return ClassicsFooter(context)
    }

    /**
     * 结束刷新
     */
    open fun hideRefreshing(hasMore: Boolean = true) {
        if (isDelayFinishLoadMore()) {
            getSmartRefreshLayout()?.finishRefresh()
        } else {
            getSmartRefreshLayout()?.finishRefresh(0)
        }
        if (hasMore) {
            if (isDelayFinishLoadMore()) {
                getSmartRefreshLayout()?.finishLoadMore()
            } else {
                getSmartRefreshLayout()?.finishLoadMore(0)
            }
        } else {
            getSmartRefreshLayout()?.finishLoadMoreWithNoMoreData()
        }
    }

    open fun autoRefresh() {
        getNestedScrollView()?.fullScroll(ScrollView.FOCUS_UP)
        getRecyclerView()?.smoothScrollToPosition(0)
        getSmartRefreshLayout()?.autoRefresh()
    }

    open fun getNestedScrollView(): NestedScrollView? {
        return null
    }

    open fun getRecyclerView(): RecyclerView? {
        return null
    }

    /**
     * 竖直滚动条是否可用
     */
    protected open fun isVerticalScrollBarEnabled() = false

    /**
     * 水平滚动条是否可用
     */
    protected open fun isHorizontalScrollBarEnabled() = false

    /**
     * 是否可以自动加载更多，默认可以
     */
    protected open fun isEnableAutoLoadMore() = true

    /**
     * 是否可以加载更多，默认可以
     */
    protected open fun isEnableLoadMore() = true

    /**
     * 是否可以刷新，默认可以
     */
    protected open fun isEnableRefresh() = true

    /**
     * 是否是纯净模式，不展示刷新头和底部，默认false
     */
    protected open fun isEnablePureScrollMode() = false

    /**
     * 刷新时是否可以越界回弹
     */
    protected open fun isEnableOverScrollBounce() = false

    /**
     * 设置是否在没有更多数据之后 Footer 跟随内容
     */
    protected open fun isEnableFooterFollowWhenNoMoreData() = false

    /**
     * 是否可拖拽
     */
    protected open fun isEnableOverScrollDrag() = false

    /**
     * 默认展示加载页面
     */
    protected open fun isShowLoadingPage() = true

    /**
     * 是否展示加载对话框
     */
    protected open fun isShowLoadingDialog() = false
}